package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.WalletInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TipPopupView;
import com.haotunet.app.goldtreasure.views.TitleView;

@Route(path = "/mine/balance")
public class MyWalletActivity extends BaseActivity implements OnClickListener {
    private TitleView mTitleView;
    private LinearLayout mFrozenLayout;
    private RelativeLayout financial_detail_layout;
    private RelativeLayout btnRecharge, btnWithDrawals;
    private TextView mBalance;
    private TextView mFrozenMoney;
    private ImageView mQuestionTip;
    private final int GET_WALLETPAGE_INFO_SUCCESS = 1;
    private WalletInfo info;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GET_WALLETPAGE_INFO_SUCCESS:
                    info = (WalletInfo) msg.obj;
                    mBalance.setText("¥" + info.getBalance());
                    if (Double.parseDouble(info.getFrozenBalance()) != 0) {
                        mFrozenLayout.setVisibility(View.VISIBLE);
                        mFrozenMoney.setText("冻结金额为" + info.getFrozenBalance() + "元");
                    }

                    break;
                default:
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        initView();
        initData();
    }

    private void initData() {
        ActionUtil actionUtil = new ActionUtil(MyWalletActivity.this);
        actionUtil.getWalletPageInfo();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                WalletInfo walletInfo = (WalletInfo) model;
                if (walletInfo == null) {
                    UIHelper.showToast(MyWalletActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (walletInfo.getErrorMsg() != null) {
                    if (walletInfo.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(MyWalletActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(MyWalletActivity.this, walletInfo.getErrorMsg());
                    }
                } else {
                    Message msg = new Message();
                    msg.what = GET_WALLETPAGE_INFO_SUCCESS;
                    msg.obj = walletInfo;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {
        mTitleView = (TitleView) findViewById(R.id.title_view_wallet);
        mTitleView.getTitletextView().setVisibility(View.VISIBLE);
        mTitleView.getTitletextView().setText("账户");
        mTitleView.getBackView().setVisibility(View.VISIBLE);
        mTitleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mFrozenLayout = (LinearLayout) findViewById(R.id.frozen_money_layout);
        mBalance = (TextView) findViewById(R.id.txt_my_balance);
        mFrozenMoney = (TextView) findViewById(R.id.txt_frozen_money);
        financial_detail_layout = (RelativeLayout) findViewById(R.id.rl_finanical_detail);
        mQuestionTip = (ImageView) findViewById(R.id.question_tip);
        btnWithDrawals = (RelativeLayout) findViewById(R.id.rl_withdrawals);
        btnRecharge = (RelativeLayout) findViewById(R.id.rl_recharge);
        mQuestionTip.setOnClickListener(this);
        financial_detail_layout.setOnClickListener(this);
        btnWithDrawals.setOnClickListener(this);
        btnRecharge.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.question_tip:
                new TipPopupView(MyWalletActivity.this,null, getString(R.string.frozen_money_tip), R.style.MyDialog).show();
                break;
            case R.id.rl_finanical_detail:
                Intent finanicalIntent = new Intent(MyWalletActivity.this, FinanicalDetailActivity.class);
                startActivity(finanicalIntent);
                break;
            case R.id.rl_recharge:
                ActivityUtils.gotoRecharge(MyWalletActivity.this);
                break;
            case R.id.rl_withdrawals:
                Intent withdrawalsIntent = new Intent(MyWalletActivity.this, WithDrawalsActivity.class);
                startActivityForResult(withdrawalsIntent, 1);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            initData();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initData();
    }
}
